﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Altairis.FluffyCloud.WebCore.Controls {

    [Themeable(true)]
    public class UserIcon : Control {
        private const int DEFAULT_SIZE = 150;

        public UserIcon() {
            this.Size = DEFAULT_SIZE;
        }

        public string CssClass {
            get { return (string)this.ViewState["CssClass"]; }
            set { this.ViewState["CssClass"] = value; }
        }

        public string NavigateUrl {
            get { return (string)this.ViewState["NavigateUrl"]; }
            set { this.ViewState["NavigateUrl"] = value; }
        }

        public int Size {
            get { return (int)this.ViewState["Size"]; }
            set {
                if (value < 1 || value > 512) throw new ArgumentOutOfRangeException("value", value, "Size must be between 1 and 80 pixels.");
                this.ViewState["Size"] = value;
            }
        }

        public string ToolTip {
            get { return (string)this.ViewState["ToolTip"]; }
            set { this.ViewState["ToolTip"] = value; }
        }

        public string UserName {
            get { return (string)this.ViewState["UserName"]; }
            set { this.ViewState["UserName"] = value; }
        }

        protected override void Render(HtmlTextWriter writer) {
            if (this.DesignMode) {
                // Render shim in design mode
                this.RenderDesignMode(writer);
                return;
            }

            // Get user name to use
            var userName = this.UserName;
            if (string.IsNullOrWhiteSpace(userName)) {
                if (this.Context.Request.IsAuthenticated) {
                    // Default to current user
                    userName = this.Context.User.Identity.Name;
                }
                else {
                    // Hide the icon
                    return;
                }
            }

            var imageUrl = this.Page.GetRouteUrl("UserIcon", new { userName = userName, iconSize = this.Size.ToString() });

            // Prepare the control to render
            Control c;
            if (string.IsNullOrWhiteSpace(this.NavigateUrl)) {
                c = new Image {
                    ImageUrl = imageUrl,
                    AlternateText = userName.ToUpper(),
                    ToolTip = this.ToolTip ?? userName.ToUpper(),
                    CssClass = this.CssClass,
                    Width = Unit.Pixel(this.Size),
                    Height = Unit.Pixel(this.Size)
                };
            }
            else {
                c = new HyperLink {
                    NavigateUrl = this.NavigateUrl,
                    ImageUrl = imageUrl,
                    Text = userName.ToUpper(),
                    ToolTip = this.ToolTip ?? userName.ToUpper(),
                    CssClass = this.CssClass,
                    Width = Unit.Pixel(this.Size),
                    Height = Unit.Pixel(this.Size),
                    ImageWidth = Unit.Pixel(this.Size),
                    ImageHeight = Unit.Pixel(this.Size),
                };
            }
            c.RenderControl(writer);
        }

        private void RenderDesignMode(HtmlTextWriter writer) {
            var c = new Image {
                AlternateText = this.UserName,
                ToolTip = this.ToolTip ?? this.UserName,
                CssClass = this.CssClass,
                Width = Unit.Pixel(this.Size),
                Height = Unit.Pixel(this.Size)
            };
            c.RenderControl(writer);
        }
    }
}